using System.Drawing;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace CosyTrade.UI
{
    public static class Functions
    {
        public static void ShowMessageBox(string messageFormat, params string[] args)
        {
            ShowMessageBox(string.Format(messageFormat, args));
        }

        public static void ShowMessageBox(string message)
        {
            XtraMessageBox.Show(message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        public static void SendTabKey(Form form)
        {
            Control currentControl = form.ActiveControl;
            form.SelectNextControl(currentControl, true, true, true, true);
            form.ActiveControl = currentControl;
        }

        public static DialogResult ShowConfigBox(string message)
        {
            return XtraMessageBox.Show(message, Application.ProductName, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
        }

        public static bool ValueIsEmpty(Control control)
        {
            if (string.IsNullOrEmpty(control.Text))
            {
                control.Focus();
                return true;
            }
            else return false;
        }

        public static string ToDBC(string input)
        {
            char[] c = input.ToCharArray();
            for (int i = 0; i < c.Length; i++)
            {
                if (c[i] == 12288)
                {
                    c[i] = (char)32;
                    continue;
                }
                if (c[i] > 65280 && c[i] < 65375)
                    c[i] = (char)(c[i] - 65248);
            }
            return new string(c);
        }

        public static byte[] GetMiniImage(string imgUrl)
        {
            Image sourceImage = Image.FromFile(imgUrl);
            Image reVal = new Bitmap(800, 800 * sourceImage.Height / sourceImage.Width);
            using (Graphics g = Graphics.FromImage(reVal))
                g.DrawImage(sourceImage, 0, 0, reVal.Width, reVal.Height);
            sourceImage.Dispose();
            using (MemoryStream ms = new MemoryStream())
            {
                BinaryFormatter formatter = new BinaryFormatter();
                formatter.Serialize(ms, reVal);
                return ms.GetBuffer();
            }
        }
    }
}